package com.github.tvbox.osc.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.tvbox.osc.R;
import com.github.tvbox.osc.base.App;
import com.github.tvbox.osc.base.BaseActivity;
import com.github.tvbox.osc.server.ControlManager;
import com.github.tvbox.osc.ui.tv.QRCodeGen;
import com.github.tvbox.osc.util.AESUtil;

import me.jessyan.autosize.utils.AutoSizeUtils;

public class PushActivity extends BaseActivity {
    private ImageView ivQRCode;
    private TextView tvAddress;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_push;
    }

    @Override
    protected void init() {
        initView();
        initData();
    }

    private void initView() {
        ivQRCode = findViewById(R.id.ivQRCode);
        tvAddress = findViewById(R.id.tvAddress);
        refreshQRCode();
        findViewById(R.id.pushLocal).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ClipboardManager manager = (ClipboardManager) PushActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                    if (manager != null) {
                        if (manager.hasPrimaryClip() && manager.getPrimaryClip() != null && manager.getPrimaryClip().getItemCount() > 0) {
                            ClipData.Item addedText = manager.getPrimaryClip().getItemAt(0);

                            if (addedText != null) {
                                //item.getText()部分手机可能会在剪切板没有相关的文本内容返回null.
                                CharSequence charSequence = addedText.getText();
                                if (charSequence != null) {
                                    String clipStr = charSequence.toString();

                                    Log.d("dozentest","copy:"+clipStr);
                                    String[] aesSplit = clipStr.split(AESUtil.shareSplit);

                                    if (aesSplit.length > 0){
                                        String aesString = aesSplit[aesSplit.length - 1];
                                        String aes = AESUtil.decryptWithAES2(AESUtil.keyMe,aesString);
                                        if (aes != null){
                                            String[] copy = aes.split(AESUtil.shareSplit);
                                            if (copy.length > 0){
                                                Intent newIntent = new Intent(mContext, DetailActivity.class);
                                                newIntent.putExtra("id", copy[0]);
                                                newIntent.putExtra("sourceKey", copy[1]);
                                                newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                                PushActivity.this.startActivity(newIntent);
                                                return;
                                            }
                                        }
                                    }
                                }
                                Toast.makeText(App.getInstance(),"复制内容有误",Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                } catch (Throwable th) {

                }
            }
        });
    }

    private void refreshQRCode() {
        String address = ControlManager.get().getAddress(false);
        tvAddress.setText(String.format("手机/电脑扫描上方二维码或者直接浏览器访问地址\n%s", address));
        ivQRCode.setImageBitmap(QRCodeGen.generateBitmap(address, AutoSizeUtils.mm2px(this, 300), AutoSizeUtils.mm2px(this, 300), 4));
    }

    private void initData() {

    }
}